﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialShare.Helpers;
using SocialShare.BLL;
using System.Configuration;

namespace SocialShare.Web.Install
{
    public partial class setupdata : System.Web.UI.Page
    {       
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private void LoadData()
        {
            ConfigReader configReader = new ConfigReader();
            try
            {
                SqlScriptRunner.RunScript(HttpContext.Current.Server.MapPath(@"~/App_Data/Clean-Data.sql"));
                SqlScriptRunner.RunScript(HttpContext.Current.Server.MapPath(@"~/App_Data/Load-Data.sql"));
                lblMessage.Text = "Congratulations! Database installation successful. Click <a href=../default.aspx>here</a> to go to default page.";
                divMessage.Attributes.Add("class", "success");
            }
            catch (Exception ex)
            {
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.OPERATION_FAILED.ToString()) + @"<br>" + ex.ToString();
                divMessage.Attributes.Add("class", "error");
            }
        }

        private void DeleteData()
        {
            ConfigReader configReader = new ConfigReader();

            try
            {
                SqlScriptRunner.RunScript(HttpContext.Current.Server.MapPath(@"~/App_Data/Clean-Data.sql"));
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.OPERATION_SUCCESS.ToString());
                divMessage.Attributes.Add("class", "success");
            }
            catch (Exception ex)
            {
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.OPERATION_FAILED.ToString()) + @"<br>" + ex.ToString();
                divMessage.Attributes.Add("class", "error");
            }
        }

        protected void btnLoadData_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void btnCleanData_Click(object sender, EventArgs e)
        {
            DeleteData();
        }
    }
}